package com.punai.standard.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.SecurityUtils;
import com.punai.standard.domain.StaClFee;
import com.punai.standard.domain.vo.StaSimplifyVo;
import com.punai.standard.mapper.StaClChapterMapper;
import com.punai.standard.mapper.StaClFeeMapper;
import com.punai.standard.mapper.StaLinkFeatureMapper;
import com.punai.standard.service.IStaClFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 材料费用项Service业务层处理
 *
 * @author zhx
 * @date 2022-12-21
 */
@Service
public class StaClFeeServiceImpl extends ServiceImpl<StaClFeeMapper,StaClFee> implements IStaClFeeService
{
    @Autowired
    private StaClFeeMapper staClFeeMapper;
    @Autowired
    private StaClChapterMapper staClChapterMapper;
    @Autowired
    private StaLinkFeatureMapper staLinkFeatureMapper;

    /**
     * 查询材料费用项列表
     *
     * @param staClFee 材料费用项
     * @return 材料费用项
     */
    @Override
    public TableDataInfo selectStaClFeeList(StaClFee staClFee)
    {
        String chapterId = staClFee.getChapterId();
        String versionId = staClFee.getVersionId();

        List<String> chapterIds = staClChapterMapper.selectChildrenIdById(chapterId, versionId);
        chapterIds.add(0, chapterId);
        PageUtils.startPage();
        List<StaClFee> staClFees = staClFeeMapper.selectListByChapterIds(staClFee, chapterIds);
        if(CollUtil.isNotEmpty(staClFees)){
            List<String> ids = staClFees.stream().map(StaClFee::getId).collect(Collectors.toList());
            List<StaSimplifyVo> simplifyVos = staLinkFeatureMapper.selectLinkFeature(ids,versionId,SecurityUtils.getCompanyId());
            Map<String, String> linkFeatureMap = simplifyVos.stream().collect(Collectors.toMap(StaSimplifyVo::getId, StaSimplifyVo::getName));
            staClFees.forEach(e -> e.setLinkFeatures(linkFeatureMap.get(e.getId())));
        }
        Long total = new PageInfo(staClFees).getTotal();
        return new TableDataInfo(staClFees, total.intValue());
    }
}
